package edu.megatrend.fkn.rsw.ocenjivanje.data;

import java.util.List;

import org.hibernate.Session;

import edu.megatrend.fkn.rsw.ocenjivanje.beans.Student;

public class DAOcenjivanje implements IOcenjivanjeData<IBean>{
	
	public static final short STUDENT_TYPE=10;
	public static final short ISPUNIO_TYPE=20;
	public static final short OBAVEZA_TYPE=30;
	public static final short OCENJUJE_TYPE=40;
	public static final short PREDAVANJE_TYPE=50;
	public static final short PREDMET_TYPE=55;
	public static final short SLUSAPRED_TYPE=60;
	public static final short SLUSAVEZBE_TYPE=70;
	public static final short VEZBE_TYPE=80;
	public static final short OCENA_TYPE=90;
	
	private static DAOcenjivanje instance=null;
	private DAOcenjivanje() {	}
	public static DAOcenjivanje getInstance(){
		if (null==instance) instance=new DAOcenjivanje();
		return instance;
	}
	
	public List<IBean> getAllBeans(short type) {
		return getAllBeans(type,"");
	}
	
	@SuppressWarnings("unchecked")
	public List<IBean> getAllBeans(short type, String query) {
		if (null==query) query="";
		Session session = DataUtils.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<IBean> result = null;
		StringBuffer resultQRY = new StringBuffer();
		switch (type){
			case STUDENT_TYPE:
				resultQRY.append("from edu.megatrend.fkn.rsw.ocenjivanje.beans.Student ");
				break;
			case ISPUNIO_TYPE:
				resultQRY.append("from edu.megatrend.fkn.rsw.ocenjivanje.beans.Ispunio ");
				break;
			case OBAVEZA_TYPE:
				resultQRY.append("from edu.megatrend.fkn.rsw.ocenjivanje.beans.Obaveza ");
				break;
			case OCENA_TYPE:
				resultQRY.append("from edu.megatrend.fkn.rsw.ocenjivanje.beans.Ocena ");
				break;
			case OCENJUJE_TYPE:
				resultQRY.append("from edu.megatrend.fkn.rsw.ocenjivanje.beans.Ocenjuje ");
				break;
			case PREDAVANJE_TYPE:
				resultQRY.append("from edu.megatrend.fkn.rsw.ocenjivanje.beans.Predavanje ");
				break;
			case PREDMET_TYPE:
				resultQRY.append("from edu.megatrend.fkn.rsw.ocenjivanje.beans.Predmet ");
				break;
			case SLUSAPRED_TYPE:
				resultQRY.append("from edu.megatrend.fkn.rsw.ocenjivanje.beans.SlusaPred ");
				break;
			case SLUSAVEZBE_TYPE:
				resultQRY.append("from edu.megatrend.fkn.rsw.ocenjivanje.beans.SlusaVezbe ");
				break;
			case VEZBE_TYPE:
				resultQRY.append("from edu.megatrend.fkn.rsw.ocenjivanje.beans.Vezbe ");
				break;
		}
		if (query.length()>0) 
			resultQRY.append(query);	
		if (resultQRY.length()>0)	
			result=session.createQuery(resultQRY.toString()).list();
		
		session.getTransaction().commit();
		return result;	
	}

	public List<IBean> getRange(short type, int indexFrom, int indexTo) {
		return getAllBeans(type, "where id between "+ indexFrom + " and "+ indexTo);
	}
	
	public void delete(IBean b/*, short type, String query*/){ 
		Session session = DataUtils.getSessionFactory().getCurrentSession();
		session.beginTransaction();
//		if (query.length()>0)
//			switch (type){
//			case STUDENT_TYPE:
//				session.createSQLQuery("DELETE FROM `student` WHERE " + query).executeUpdate();
//				break;
//			case ISPUNIO_TYPE:
//				session.createSQLQuery("DELETE FROM `ispunio` WHERE " + query).executeUpdate();
//				break;
//			case OBAVEZA_TYPE:
//				session.createSQLQuery("DELETE FROM `obaveza` WHERE " + query).executeUpdate();
//				break;
//			case OCENA_TYPE:
//				session.createSQLQuery("DELETE FROM `ocena` WHERE " + query).executeUpdate();
//				break;
//			case OCENJUJE_TYPE:
//				session.createSQLQuery("DELETE FROM `ocenjuje` WHERE " + query).executeUpdate();
//				break;
//			case PREDAVANJE_TYPE:
//				session.createSQLQuery("DELETE FROM `predavanje` WHERE " + query).executeUpdate();
//				break;
//			case PREDMET_TYPE:
//				session.createSQLQuery("DELETE FROM `predmet` WHERE " + query).executeUpdate();
//				break;
//			case SLUSAPRED_TYPE:
//				session.createSQLQuery("DELETE FROM `slusapred` WHERE " + query).executeUpdate();
//				break;
//			case SLUSAVEZBE_TYPE:
//				session.createSQLQuery("DELETE FROM `slusavezbe` WHERE " + query).executeUpdate();
//				break;
//			case VEZBE_TYPE:
//				session.createSQLQuery("DELETE FROM `vezbe` WHERE " + query).executeUpdate();
//				break;
//			}
		session.delete(b);
		session.getTransaction().commit();
	}
		
	public void update(IBean b){
		Session session = DataUtils.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		session.merge(b);
		
		session.getTransaction().commit();		
	}
	public static void main (String args []){
		DAOcenjivanje ocenjivanje = DAOcenjivanje.getInstance();
		List<IBean> lista = ocenjivanje.getAllBeans(DAOcenjivanje.STUDENT_TYPE,"WHERE ime='Dragan'");
		for (IBean bean : lista){
			System.out.println( ((Student)bean).getIme()+ " " +((Student)bean).getPrezime());
		}
	}
}
